﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace Taxes.Industory
{
    public partial class IndustoryImport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [DirectMethod]
        public void DeleteFile(string fileName, string fileClientName)
        {
            try
            {
                System.IO.File.Delete(fileName);
                //X.Msg.Notify("提示", fileClientName + "已删除!").Show();
            }
            catch (Exception ex)
            {
                //X.Msg.Notify("提示", fileClientName + "删除失败!").Show();
            }
        }
        [DirectMethod]
        public void Up(string att)
        {
            var path = System.Configuration.ConfigurationManager.AppSettings["UploadPath"].ToString();
            var fileName = Server.MapPath(path + "/" + att);

            List<Trade> trades = new List<Trade>();
            #region 解析excel

            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                var workBook = new HSSFWorkbook(stream);

                var sheetEnumerator = workBook.GetEnumerator();
                while (sheetEnumerator.MoveNext())
                {
                    var sheet = sheetEnumerator.Current as HSSFSheet;
                    if (sheet == null)
                        continue;
                    var rowEnumerator = sheet.GetEnumerator();

                    while (rowEnumerator.MoveNext())
                    {
                        var row = rowEnumerator.Current as HSSFRow;
                        if (row == null)
                            continue;
                        //正式内容从第三行开始
                        if (row.RowNum >= 2)
                        {
                            var cellEnumerator = row.GetEnumerator();
                            Trade trade = new Trade();
                            while (cellEnumerator.MoveNext())
                            {
                                var cell = cellEnumerator.Current as HSSFCell;
                                if (cell == null)
                                    continue;
                                switch (cell.ColumnIndex)
                                {
                                    case 0:
                                        if (cell.StringCellValue.ToLower() == "y")
                                        {
                                            trade.IsBigTrade = true;
                                        }
                                        break;
                                    case 1:
                                        {
                                            trade.ParentCode = cell.StringCellValue;
                                        }
                                        break;
                                    case 2:
                                        {
                                            trade.TypeCode = cell.StringCellValue;
                                        }
                                        break;
                                    case 3:
                                        trade.Name = cell.StringCellValue;
                                        break;

                                    case 4:
                                        if (cell.StringCellValue.ToLower() == "y")
                                        {
                                            trade.IsMenLei = true;
                                        }
                                        break;
                                }
                            }
                            trades.Add(trade);
                        }
                    }
                }
            }


            #endregion


            var bll = new BLL.industry_info();

            var menlei = (from c in trades where c.IsMenLei == true select c).ToList();

            foreach (var r in menlei)
            {
                Model.industry_info info = new Model.industry_info()
                {
                    industry_name = r.Name,
                    industry_no = r.TypeCode,
                };
                var id = bll.Add(info);
                var dalei = (from d in trades where (d.ParentCode == r.TypeCode&& d.IsMenLei==false) select d).ToList();

                foreach (var d in dalei)
                {
                    Model.industry_info daLeiInfo = new Model.industry_info()
                    {
                        industry_name = d.Name,
                        industry_no = d.TypeCode,
                        parent_id = id
                    };
                    var daleiID = bll.Add(daLeiInfo);
                    var xiaolei = (from x in trades where x.ParentCode == d.TypeCode select x).ToList();
                    foreach (var x in xiaolei)
                    {
                        Model.industry_info xiaoLeiInfo = new Model.industry_info()
                       {
                           industry_name = x.Name,
                           industry_no = x.TypeCode,
                           parent_id = daleiID
                       };
                        bll.Add(xiaoLeiInfo);
                    }
                }
            }
            Response.Redirect("IndustoryManager.aspx");
        }
    }
}